package com.nio.page;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;

/**
 * Created by houlian.wang on 2016/12/1.
 */

public class AirSeekBar extends SeekBar implements View.OnTouchListener, SeekBar.OnSeekBarChangeListener{
    private static final String TAG = "AirSeekBar";
    public AirSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnSeekBarChangeListener(this);
        setOnTouchListener(this);
    }

    /*@Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
            getParent().requestDisallowInterceptTouchEvent(true);
            getParent().getParent().requestDisallowInterceptTouchEvent(true);
            getParent().getParent().getParent().requestDisallowInterceptTouchEvent(true);
            Log.d(TAG, "dispatchTouch:" + action);
        }
        return super.dispatchTouchEvent(event);
    }*/

    /**
     * Called when a touch event is dispatched to a view. This allows listeners to
     * get a chance to respond before the target view.
     *
     * @param view     The view the touch event has been dispatched to.
     * @param event The MotionEvent object containing full information about
     *              the event.
     * @return True if the listener has consumed the event, false otherwise.
     */
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
            getParent().requestDisallowInterceptTouchEvent(true);
            //getParent().getParent().requestDisallowInterceptTouchEvent(true);
            //getParent().getParent().getParent().requestDisallowInterceptTouchEvent(true);
            Log.d(TAG, "onTouch:" + action);
        } else {
            getParent().requestDisallowInterceptTouchEvent(false);
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //boolean result = super.onTouchEvent(event);
        Log.d(TAG, "onTouchEvent2:" + event.getAction() + ",isEnable:" + isEnabled());
        if (event.getAction() == MotionEvent.ACTION_DOWN) {

        }
        return super.onTouchEvent(event);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        Log.d(TAG, "AirSeekBar2 onProgressChanged:" + getProgress());
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        Log.d(TAG, "AirSeekBar2 onStartTrackingTouch:" + getProgress());
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        Log.d(TAG, "AirSeekBar2 onStopTrackingTouch:" + getProgress());
    }
}
